package per.chenxin.apathy.KCTTBLs;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import per.chenxin.apathy.Util;

public class Gb2Jis {

    public final static void gb2jis(String strInput, String strOutput) throws IOException {

        gb2jis(strInput, strOutput, "GBK");
    }

    public final static void gb2jis(String strInput, String strOutput, String inputFileCode) throws IOException {

        InputStream input = new FileInputStream(strInput);
        PrintWriter output = new PrintWriter(strOutput,"SJIS");
        gb2jis(input, output, inputFileCode);
    }

    public final static void gb2jis(String strInput, String strOutput, String inputFileCode, String outputFileCode) throws IOException {

        InputStream input = new FileInputStream(strInput);
        PrintWriter output = new PrintWriter(strOutput,outputFileCode);
        gb2jis(input, output, inputFileCode);
    }


    public final static void gb2jis(InputStream input, PrintWriter output) throws IOException {

        gb2jis(input, output, "GBK");

    }

    public final static String gb2jis(String input) throws IOException {
        char[] chars = new char[input.length()];
        input.getChars(0, input.length(), chars, 0);
        StringBuffer sb = new StringBuffer();
        for (char c1 : chars) {
            String str = getValueFromCode(c1, "GBK", "A0");
            if (str == null) {
                str = new String(new char[] {
                    c1
                });
            }
            sb.append(str);
        }
        return sb.toString();
    }

    public final static void gb2jis(InputStream input, PrintWriter output, String fileCode) throws IOException {

        BufferedReader bufferedReader = new LineNumberReader(new InputStreamReader(input, fileCode));
        String strLine = null;
        while ((strLine = bufferedReader.readLine()) != null) {
            char[] chars = new char[strLine.length()];
            strLine.getChars(0, strLine.length(), chars, 0);
            for (char c1 : chars) {
                String str = getValueFromCode(c1, "GBK", "A0");
                if (str == null) {
                    str = new String(new char[] {
                        c1
                    });
                }
                output.print(str);
            }
            output.println();
            output.flush();
        }
        output.println();
        output.flush();

    }

    public final static String STR_GAIJCONV_TBL = "GAIJCONV.TBL";

    public final static String getValueFromCode(byte[] bytes, String strEscape) {

        return map.get(Util.encodeStringTo10(Util.code16ToZone(bytes, strEscape)));
    }

    public final static String getValueFromCode(String str, String code, String strEscape)
        throws UnsupportedEncodingException {

        return map.get(Util.encodeStringTo10(Util.code16ToZone(str.getBytes(code), strEscape)));
    }

    public final static String getValueFromCode(char c, String code, String strEscape)
        throws UnsupportedEncodingException {

        return map.get(Util.stringToZone(c, code, strEscape));
    }

    public final static String getValueFromZone(String strZone) {

        return map.get(strZone);
    }

    public final static Map<String, String> map;
    static {
        map = new HashMap<String, String>();
        try {

            BufferedReader bufferedReader = new LineNumberReader(new InputStreamReader(Gb2Jis.class
                .getResourceAsStream(Gb2Jis.class.getSimpleName() + ".TBL"), "UTF-8"));
            String strLine = null;
            while ((strLine = bufferedReader.readLine()) != null) {
                if (strLine.trim().length() == 0)
                    continue;
                if (strLine.startsWith(";"))
                    continue;
                String strZone = strLine.substring(0, 4);
                String strValue = strLine.substring(4);

                if (strValue.trim().length() > 0)
                    map.put(strZone, strValue.substring(0, 1));
            }

            bufferedReader = new LineNumberReader(new InputStreamReader(Gb2Jis.class
                .getResourceAsStream(STR_GAIJCONV_TBL), "UTF-8"));
            while ((strLine = bufferedReader.readLine()) != null) {
                if (strLine.trim().length() == 0)
                    continue;
                if (strLine.startsWith(";"))
                    continue;
                String strs[] = strLine.split(",");
                if ("9999".equals(strs[2]))
                    continue;
                if (map.containsKey(strs[2]))
                    continue;
                if (strs[4].trim().length() > 0)
                    map.put(strs[2], strs[4]);
            }

        } catch (UnsupportedEncodingException e) {
        } catch (IOException e) {
        }

    }

}
